
static char RCSId[]="$Id: TicOptionsPanel.m,v 1.1.1.1 1993/03/18 03:36:32 davis Exp $";


#import <appkit/Application.h>
#import <appkit/Button.h>
#import <appkit/Form.h>
#import <appkit/Matrix.h>
#import <appkit/PopUpList.h>
#import <appkit/TextField.h>

#import "CellScrollView.h"
#import "GnuplotPlot.h"
#import "Status.h"
#import "StatusTics.h"
#import "TicCell.h"
#import "TicObject.h"
#import "TicOptionsPanel.h"


@interface TicOptionsPanel (Private)

- _updatePanel;			/** Overridden from OptionsPanel (Private) **/

@end


@implementation TicOptionsPanel

- init
{
    [super init];

    [NXApp loadNibSection: "TicOptionsPanel.nib"
		    owner: self
		withNames: NO
		 fromZone: [self zone]];

    [panel setFrameUsingName:"TicOptionsPanel"];
    [panel setFrameAutosaveName:"TicOptionsPanel"];

    /*  Initialize custom tics CellScrollView  */

    [userScrollView initMatrix:[TicCell class]];
    userMatrix = [userScrollView cellMatrix];
    [[userMatrix setAction:@selector(selectTic:)] setTarget:self];
    lastCoord = [coordButton tag];

    return self;
}


- changeCoord:sender
{
    [coordButton setTag:[sender selectedTag]];
    [self _updatePanel];
    return self;
}



- doSetTicsType:sender
{
    [status setTicTypeCoord:[[coordButton target] indexOfItem:
			     [coordButton title]] to:[typeMatrix selectedTag]];
    [self _updatePanel];
    return self;
}


- doSetTicsSeries:sender
{
    int coord = [coordButton tag];
    double start = [startField doubleValue];
    double incr = [incrementField doubleValue];
    double end = [endField doubleValue];
    int index = [sender selectedIndex];

    /* 
     *  The increment must take us from the start to the end.  And the 
     *  end must be larger than the start (gnuplot help says this is 
     *  not true, but it doesn't seem to work otherwise).
     */
    if (((start + incr) <= start) || (start >= end))
	[self _updatePanel];

    else {
	id selectedCell = [sender selectedCell];

	if (selectedCell == startField)
	    [status setTicStartCoord:coord to:start];
	else if (selectedCell == incrementField)
	    [status setTicIncrementCoord:coord to:incr];
	else
	    [status setTicEndCoord:coord to:end];
    }

    return self;
}


- addTic:sender
{
    int coord = [coordButton tag];
    TicObject *ticObject;
    int count;

    ticObject = [[TicObject allocFromZone:[status zone]]
			   initFromString:[userTitleForm stringValueAt:0]
			      doubleValue:[userValueForm doubleValueAt:0]];
    [ticsList[coord] addObject:ticObject];
    [userScrollView loadCellsFrom:ticsList[coord]];

    count = [ticsList[coord] count] - 1;
    [userMatrix scrollCellToVisible:count :0];
    [userMatrix selectCellAt:count:0];

    [self selectTic:self];
    [status reportSettingsChange:self];

    return self;
}


- deleteTics:sender
{
    int coord = [coordButton tag];
    int i;

    for (i=[userMatrix cellCount]-1; i>=0; i--) {
	TicCell *cell = [userMatrix cellAt:i:0];
	if ([cell isHighlighted]) {
	    /*
	     *  If a cell is highlighted, remove (and free) the 
	     *  corresponding item from the list of TicObjects.
	     */
	    [[ticsList[coord] removeObject:[cell subObject]] free];
	}
    }

    [userScrollView loadCellsFrom:ticsList[coord]];

    [userMatrix scrollCellToVisible:0 :0];
    [userMatrix selectCellAt:0:0];

    [self selectTic:self];
    [status reportSettingsChange:self];
    return self;
}

    
- modifyTic:sender
{
    int coord = [coordButton tag];
    TicCell *cell = [userMatrix selectedCell];
    TicObject *tic = [cell subObject];

    [tic setStringValue: [userTitleForm stringValueAt:0]];
    [tic setDoubleValue:[userValueForm doubleValueAt:0]];
    [userScrollView loadCellsFrom:ticsList[coord]];
    [userMatrix selectCell:cell];

    [self selectTic:self];
    [status reportSettingsChange:self];
    return self;
}


/* 
 *  Make other controls in the window reflect the currently selected 
 *  tic.  Assumes that the current tic type is TIC_USER.
 */
- selectTic:sender
{
    TicObject *tic = [[userMatrix selectedCell] subObject];

    if (tic) {
	[userTitleForm setStringValue:[tic stringValue] at:0];
	[userValueForm setDoubleValue:[tic doubleValue] at:0];
    } else {
	[userTitleForm setStringValue:"" at:0];
	[userValueForm setStringValue:"" at:0];
    }

    [deleteTicButton setEnabled:tic? YES: NO];
    [modifyTicButton setEnabled:tic? YES: NO];
    [userValueForm selectTextAt:0];

    return self;
}


// Shuts up the compiler about unused RCSId
- (const char *) rcsid
{
    return RCSId;
}


@end





@implementation TicOptionsPanel (Private)


- _updatePanel
{
    int coord = [coordButton tag];
    int type = TIC_COMPUTED;
    BOOL isEnabled;

    [panel disableDisplay];

    /* 
     *  If the current status is not nil, update the values of all the 
     *  controls.
     */
    if (status) {

	int i;
	BOOL isThreeD = [status isThreeD];

        for (i = 0 ; i < 3 ; i++)			//
            ticsList[i] = [status ticDefsCoord:i];	//

	if (!isThreeD && (coord == Z_TAG))  {
	    id cell = [[coordButton target] findCellWithTag:X_TAG];
	    [coordButton setTitle:[cell title]];
	    [coordButton setTag:coord = [cell tag]];
	}

	type = [status ticTypeCoord:coord];
	[typeMatrix selectCellWithTag:type];

	/* Uniform Range (Series) section */

	[startField setDoubleValue:[status ticStartCoord:coord]];
	[incrementField setDoubleValue:[status ticIncrementCoord:coord]];
	[endField setDoubleValue:[status ticEndCoord:coord]];

	/* User-Defined Range section */

	if ([userMatrix selectedCell])
	    [self selectTic:self];

	[userScrollView loadCellsFrom:ticsList[coord]];
	lastCoord = coord;
	
	/* 
	 *  Enabling/Disabling of the Z coordinate button in the pop 
	 *  up list.
	 */
	
	[[[coordButton target] findCellWithTag:Z_TAG] setEnabled:isThreeD];

    }

    /* Now do enabling/disabling */

    [typeMatrix setEnabled: isEnabled = (status? YES : NO)];
    [coordButton setEnabled: isEnabled];

    [startField setEnabled:isEnabled = ((type == TIC_SERIES) && status)];
    [incrementField setEnabled:isEnabled];
    [endField setEnabled:isEnabled];

    [addTicButton setEnabled:isEnabled = ((type == TIC_USER) && status)];
    [userTitleForm setEnabled:isEnabled];
    [userValueForm setEnabled:isEnabled];
    [userMatrix setEnabled:isEnabled];

    [deleteTicButton setEnabled:isEnabled = [userMatrix selectedCell]
					    && status];
    [modifyTicButton setEnabled:isEnabled];


    switch (type) {
    case TIC_USER:
	[userValueForm selectTextAt:0];
	break;
    case TIC_SERIES:
	[seriesForm selectTextAt:0];
	break;
    }

    [panel reenableDisplay];
    [panel display];

    return self;
}


@end
